package com.wgz.flag.controller;

import com.wgz.flag.common.Result;
import com.wgz.flag.domain.dto.GroupMessageDTO;
import com.wgz.flag.domain.vo.GroupMessageVO;
import com.wgz.flag.service.GroupMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group/message")
@CrossOrigin(origins = {"http://localhost:8081","http://localhost:8082"}, allowCredentials = "true")
@RequiredArgsConstructor
public class GroupMessageController {

    private final GroupMessageService groupMessageService;

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public Result<GroupMessageVO> sendMessage(@RequestParam Long senderId, 
                                            @RequestBody GroupMessageDTO messageDTO) {
        GroupMessageVO message = groupMessageService.sendMessage(senderId, messageDTO);
        return Result.success(message);
    }

    /**
     * 获取群聊消息历史
     */
    @GetMapping("/history")
    public Result<List<GroupMessageVO>> getMessageHistory(@RequestParam Long groupId, 
                                                        @RequestParam(defaultValue = "200") Long lastMessageId) {
        List<GroupMessageVO> messages = groupMessageService.getMessageHistory(groupId, lastMessageId);
        return Result.success(messages);
    }

    /**
     * 撤回消息
     */
    @DeleteMapping("/recall")
    public Result<String> recallMessage(@RequestParam Long messageId, @RequestParam Long operatorId) {
        groupMessageService.recallMessage(messageId, operatorId);
        return Result.success("消息已撤回");
    }
}